﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace HJ_SQL_ToTable
{
    public class ExtractSQLLineUtil
    {
        string rule_table = "CREATE TABLE `([^`]*)`";

        string rule_field = "`([^`]*)` ([^_]*)_([0-9]*)_";
        string rule_notnull = "NOT NULL";

        string rule_primary = " PRIMARY KEY _`([^`]*)`";

        string rule_engine = "ENGINE=([^ ;]*)";
        string rule_charset = "CHARSET=([^ ;]*)";
        

        public ExtractSQLLineResult Results = new ExtractSQLLineResult();

        public string ErrMsg = string.Empty;

        public bool Extract(string line)
        {
            bool bRet = false;
            var strLine = line.Replace("(", "_");
            strLine = strLine.Replace(")", "_");
            do
            {
                if (CheckTable(line))
                {
                    bRet = true;
                    break;
                }

                if (CheckField(strLine))
                {
                    if (CheckNotnull(strLine))
                    {

                    }

                    bRet = true;
                    break;
                }

                if (CheckPrimary(strLine))
                {
                    bRet = true;
                    break;
                }

                if (CheckEngine(strLine))
                {
                    if (CheckCharset(strLine)) { }

                    bRet = true;
                    break;
                }

            } while (false);

            return bRet;
        }

        public bool CheckTable(string line)
        {
            MatchCollection mts_;

            bool exitLoop = false;
            try
            {
                mts_ = Regex.Matches(line, rule_table);

                for (int i = 0; i < mts_.Count; i++)
                {
                    Match mt = mts_[i];
                    var value = mt.Groups[1].Value;
                    Results.IsTableName = true;
                    Results.Name = value;
                    exitLoop = true;
                    break;
                }

            }
            catch (Exception ex)
            {

            }
            return exitLoop;
        }

        public bool CheckField(string line)
        {
            MatchCollection mts_;

            bool exitLoop = false;
            try
            {
                mts_ = Regex.Matches(line, rule_field);

                for (int i = 0; i < mts_.Count; i++)
                {
                    Match mt = mts_[i];
                    if (mt.Groups.Count == 4)
                    {
                        //var value = mt.Groups[1].Value;
                        Results.FieldName = mt.Groups[1].Value;
                        Results.FieldType = mt.Groups[2].Value;
                        Results.FieldLength = Convert.ToInt32(mt.Groups[3].Value);
                        Results.IsField = true;
                        exitLoop = true;
                        break;
                    }

                }

            }
            catch (Exception ex)
            {

            }
            return exitLoop;
        }
        public bool CheckPrimary(string line)
        {
            MatchCollection mts_;

            bool exitLoop = false;
            try
            {
                mts_ = Regex.Matches(line, rule_primary);

                for (int i = 0; i < mts_.Count; i++)
                {
                    Match mt = mts_[i];
                    if (mt.Groups.Count == 2)
                    {
                        //var value = mt.Groups[1].Value;
                        Results.IsPrimaryKey = true;
                        Results.Key = mt.Groups[1].Value;
                        exitLoop = true;
                        break;
                    }

                }

            }
            catch (Exception ex)
            {

            }
            return exitLoop;
        }

        public bool CheckEngine(string line)
        {
            MatchCollection mts_;

            bool exitLoop = false;
            try
            {
                mts_ = Regex.Matches(line, rule_engine);

                for (int i = 0; i < mts_.Count; i++)
                {
                    Match mt = mts_[i];
                    if (mt.Groups.Count == 2)
                    {
                        //var value = mt.Groups[1].Value;
                        Results.Name = mt.Groups[1].Value;
                        Results.Key = "Engine";
                        exitLoop = true;
                        break;
                    }

                }

            }
            catch (Exception ex)
            {

            }
            return exitLoop;
        }

        public bool CheckCharset(string line)
        {
            MatchCollection mts_;

            bool exitLoop = false;
            try
            {
                mts_ = Regex.Matches(line, rule_charset);

                for (int i = 0; i < mts_.Count; i++)
                {
                    Match mt = mts_[i];
                    if (mt.Groups.Count == 2)
                    {
                        //var value = mt.Groups[1].Value;
                        Results.Name = mt.Groups[1].Value;
                        Results.Key = "Charset";
                        exitLoop = true;
                        break;
                    }

                }

            }
            catch (Exception ex)
            {

            }
            return exitLoop;
        }

        public bool CheckNotnull(string line)
        {
            MatchCollection mts_;

            bool exitLoop = false;
            try
            {
                mts_ = Regex.Matches(line, rule_notnull);

                for (int i = 0; i < mts_.Count; i++)
                {
                    //var value = mt.Groups[1].Value;
                    Results.CanSetNull = false;
                    //Results.FieldName = mt.Groups[1].Value;
                    exitLoop = true;
                    break;
                }

            }
            catch (Exception ex)
            {

            }
            return exitLoop;
        }
    }
}
